<?php if(!defined('OCTOMS')){header('HTTP/1.1 403');die('{"error":"forbidden"}');}
/*
 * @package       OctoMS
 * @subpackage	DesignJotter
 * @link          http://DesignJotter.com
 * @copyright     Copyright 2012, Valentino-Jivko Radosavlevici
 * @license       GPL v3.0 (http://www.gnu.org/licenses/gpl-3.0.txt)
 * 
 * Redistributions of files must retain the above copyright notice.
 * 
 * @since         OctoMS 0.0.1, DesignJotter 2.0.1
 */
	
	/*
	 * Facebook widget
	 * 
	 * @package OctoMS.com
	 * @subpackage DesignJotter
	 * @link http://DesignJotter.com
	 * @license       GPL v3.0 (http://www.gnu.org/licenses/gpl-3.0.txt)
	 * @version 0.1
	 * 
	 * @author Valentino-Jivko Radosavlevici
	 */
	class w_facebook extends octoms
	{
		function __construct()
		{
			// Load the view core library
			$this->_('view');
		}
		
		/**
		 * Initialized?
		 */
		static $initalized = false;
		
		/**
		 * Initialization function
		 * 
		 * @param array $data
		 * 
		 * @author Valentino-Jivko Radosavlevici
		 */
		function _init($data=array())
		{
			if (!self::$initalized)
			{
				$data = (array)$data;
				if (!isset($data['appId']) || empty($data['appId'])) 
				{
					// Get the default application id
					$fb = $this->settings->get('facebook');
					if (isset($fb['appId']) && !empty($fb['appId']))
					{
						$data['appId'] = $fb['appId'];
					}
				}
				
				// Print the facebook initialization script
				$this->view->display(
					'fb-sdk',
					array(
						'data'=>$data
					)
				);
				
				// Stop here
				self::$initalized = true;
			}
			
			
		}// end function _init()
		
		/**
		 * Facebook Comments
		 * Add a Facebook comment box to your page.
		 * Manage your <a href="{fb_page}">App ID ({id})</a>.
		 * 
		 * @param int $number 10 [The number of posts to show]
		 * @param int $width [The box's width in pixels (optional)]
		 * @param string $theme [Color scheme]<ul>
		 * <li>light</li>
		 * <li>dark</li>
		 * </ul>
		 * 
		 * @author Valentino-Jivko Radosavlevici
		 */
		function comments($number=10,$width=null,$theme='light',$url=null,$appId=null)
		{
			// Initialize the FB SDK
			$this->_init(
				array(
					'appId'=>$appId
				)
			);
			
			// Load the JS
			$this->template->scriptroot('facebook');
			
			// Prepare the attributes
			$atts = array();
			
			// Correct the number
			$number = intval($number);
			if ($number != 0) $atts['data-num-posts'] = $number;
			
			// Correct the width
			$width = intval($width);
			if ($width != 0) 
			{
				$atts['data-width'] = $width;
			}
			else 
			{
				$this->template->css('fb-fix');
			}
			
			// 2 themes available
			if (!in_array($theme, array('light','dark')))
			{
				$theme = 'light';
			}
			$atts['data-colorscheme'] = $theme;
			
			// Set the class
			$atts['class'] = 'fb-comments';
			
			// Always fill the width of the parent element
			$atts['style'] = 'width:100%';
			
			// And last but not least, the URL
			$atts['data-href'] = (is_null($url))?current_url():$url;
			
			// Print the comments box
			print tag('div',$atts);
			
		}// end function comments()
		
		function _h_comments()
		{
			// Prepare the result
			$result = array();
			
			// Get the current App Id
			$fb = octoms('settings',OMS_L)->get('facebook');
			if (isset($fb['appId']) && !empty($fb['appId'])) 
			{
				$result['id'] = '<b>'.$fb['appId'].'</b>';
			}
			else 
			{
				$result['id'] = '<b>not set</b>';
			}
			
			// Get the facebook config page
			$result['fb_page'] = baseurl('_main/facebook');
			
			// All done
			return $result;
		}
		
	}// end class facebook
	
	
/* End Of File <w_facebook.inc> */